# -*- coding: utf-8 -*-
# @Date    : 2017-04-25 17:17:11
# @Author  : fancy (fancy@thecover.co)
import time

from app import app

DAY_SEC = 60*60*24

@app.template_filter('dateformat')
def cus_dateformat(value, format='%Y-%m-%d'):
    gap = time.time() - time.mktime(value.timetuple())
    if gap < 60:
        return u'%d 秒前' % gap
    elif gap < 3600:
        return u'%d 分钟前' % (gap / 60.0 + 1 if gap % 60 else 0)
    elif gap < DAY_SEC:
        hour = gap / 3600
        minute = gap % 3600 / 60.0
        return u'%d 小时 %d 分钟前' % (hour, minute)
    return value.strftime(format)

